<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>仓库管理</title>
    <!-- Bootstrap CSS-->
    <link rel="stylesheet" href="../../static/css/lib/bootstrap3.4.1.min.css" th:href="@{/css/lib/bootstrap3.4.1.min.css}">
    <!-- Bootstrap-table CSS-->
    <link rel="stylesheet" href="../../static/css/lib/bootstrap-table.min.css" th:href="@{/css/lib/bootstrap-table.min.css}">
    <!-- jquery-ui CSS-->
    <link rel="stylesheet" href="../../static/css/lib/jquery-ui.min.css" th:href="@{/css/lib/jquery-ui.min.css}">
    <!-- select2 CSS-->
    <link rel="stylesheet" href="../../static/css/lib/select2.min.css" th:href="@{/css/lib/select2.min.css}">
    <link rel="stylesheet" href="../../static/css/lib/select2-bootstrap.min.css" th:href="@{/css/lib/select2-bootstrap.min.css}">
    <link href="../../static/css/file.css" rel="stylesheet" th:href="@{/css/file.css}">
    <link href="../../static/css/table.css" rel="stylesheet" th:href="@{/css/table.css}">
</head>

<body onload="init();">

    <div class="panel-body" style="padding-bottom:0;">
        <div class="panel panel-default">
            <div class="panel-heading">查询条件</div>
            <div class="panel-body">
                <div class="form-group" style="margin-top:15px">
                    <div class="row row-gap">
                        <label class="control-label col-sm-2 label-font" for="txt_search_house_manager">管理员</label>
                        <div class="col-sm-4">
                            <input type="text" class="form-control" id="txt_search_house_manager">
                        </div>

                        <label class="control-label col-sm-2 label-font" for="txt_search_name">仓库名称</label>
                        <div class="col-sm-4">
                            <input type="text" class="form-control" id="txt_search_name">
                        </div>
                    </div>

                    <div class="row row-gap">
                        <label class="control-label col-sm-2 label-font" for="txt_search_type">仓库类型</label>
                        <div class="col-sm-4">
                            <select class="form-control" id="txt_search_type">
                                <option value="--请选择--">--请选择--</option>
                                <option value="成品仓库">成品仓库</option>
                                <option value="材料仓库">材料仓库</option>
                            </select>
                        </div>

                        <label class="control-label col-sm-2 label-font" for="txt_search_is_zero">是否零品仓</label>
                        <div class="col-sm-4">
                            <select class="form-control" id="txt_search_is_zero">
                                <option value="--请选择--">--请选择--</option>
                                <option value="是">是</option>
                                <option value="否">否</option>
                            </select>
                        </div>
                    </div>
                    <div class="row row-gap">
                        <div class="col-sm-offset-4" style="text-align:left;">
                            <button type="button" style="margin-left:50px" id="btn_filter" class="btn btn-primary" onclick="filter();">过滤</button>
                            <button type="button" style="margin-left:50px" id="btn_reset_filter" class="btn btn-primary" onclick="init();">重置过滤</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div id="toolbar" class="btn-group">
            <button id="btn_add" type="button" class="btn btn-default" data-toggle="modal" data-target="#addModal" onclick="">
                <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增仓库
            </button>
        </div>
        <table id="table"></table>
    </div>

    <!-- 模态框（Modal） 查看-->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                        &times;
                    </button>
                    <h4 class="modal-title" id="myModalLabel">
                        查看仓库信息
                    </h4>
                </div>
                <div class="modal-body">
                    <table class="table">
                        <caption>仓库信息</caption>
                        <tbody>
                        <tr>
                            <td>仓库名称</td>
                            <td id="td_name">xxx</td>
                        </tr>
                        <tr>
                            <td>仓库类型</td>
                            <td id="td_type">xxx</td>
                        </tr>
                        <tr>
                            <td>是否零品仓 </td>
                            <td id="td_is_zero">xxx</td>
                        </tr>
                        <tr>
                            <td>管理员 </td>
                            <td id="td_house_manager">xxx</td>
                        </tr>
                        <tr>
                            <td>仓库地址 </td>
                            <td id="td_address">xxx</td>
                        </tr>
                        <tr>
                            <td>仓库用途 </td>
                            <td id="td_use">xxx</td>
                        </tr>
                        </tbody>
                    </table>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                    </button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </div>

    <!-- 模态框（Modal） 新增 -->
    <div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="addModalLabel" aria-hidden="true">
        <div class="modal-dialog" style="min-width: 800px">
            <div class="modal-content">
                <div class="modal-header">
                    <h2 class="modal-title text-center" id="addModalLabel">
                        添加仓库
                    </h2>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" role="form" onsubmit="return false;">

                        <div class="form-group has-error">
                            <label class="col-sm-2 control-label label-font" for="txt_name">仓库名称</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="txt_name" title="必填选项" onchange="checkInputChange('txt_name');">
                            </div>
                        </div>

                        <div class="form-group has-error">
                            <label class="col-sm-2 control-label label-font" for="txt_type">仓库类型</label>
                            <div class="col-sm-10">
                                <select class="form-control" data-style="btn-danger" id="txt_type" title="必填选项" onchange="checkInputChange('txt_type', '--请输入--');">
                                    <option>--请输入--</option>
                                    <option>成品仓库</option>
                                    <option>材料仓库</option>
                                </select>
                            </div>
                        </div>

                        <div class="form-group has-error">
                            <label class="col-sm-2 control-label label-font" for="txt_is_zero">是否零品仓</label>
                            <div class="col-sm-10">
                                <select class="form-control" data-style="btn-danger" id="txt_is_zero" onchange="checkInputChange('txt_is_zero', '--请输入--');">
                                    <option>--请输入--</option>
                                    <option>是</option>
                                    <option>否</option>
                                </select>
                            </div>
                        </div>

                        <div class="form-group has-error">
                            <label class="col-sm-2 control-label label-font" for="user_select">管理员</label>
                            <div class="col-sm-10">
                                <select class="js-data-example-ajax form-control" id="user_select" onchange="setInputSuccess($('#user_select'));">
                                </select>
                            </div>
                        </div>

                        <div class="form-group has-success">
                            <label class="col-sm-2 control-label label-font" for="txt_address">仓库地址</label>
                            <div class="col-sm-10">
                                <textarea class="form-control" rows="4" id="txt_address"></textarea>
                            </div>
                        </div>

                        <div class="form-group has-success">
                            <label class="col-sm-2 control-label label-font" for="txt_use">仓库用途</label>
                            <div class="col-sm-10">
                                <textarea class="form-control" rows="4" id="txt_use"></textarea>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-success" onclick="addMsg();">添加</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </div>

    <!--编辑模态框（Modal） 编辑-->
    <div class="modal fade" id="alterModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog" style="min-width: 800px">
            <div class="modal-content">
                <div class="modal-header">
                    <h2 class="modal-title text-center" id="alterModalLabel">
                        编辑仓库信息
                    </h2>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" role="form" onsubmit="return false;">

                        <div class="form-group has-success">
                            <label class="col-sm-2 control-label label-font" for="alter_name">仓库名称</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="alter_name" title="必填选项" onchange="checkInputChange('alter_name');">
                            </div>
                        </div>

                        <div class="form-group has-success">
                            <label class="col-sm-2 control-label label-font" for="alter_type">仓库类型</label>
                            <div class="col-sm-10">
                                <select class="form-control" data-style="btn-danger" id="alter_type">
                                    <option>成品仓库</option>
                                    <option>材料仓库</option>
                                </select>
                            </div>
                        </div>

                        <div class="form-group has-success">
                            <label class="col-sm-2 control-label label-font" for="alter_is_zero">是否零品仓</label>
                            <div class="col-sm-10">
                                <select class="form-control" data-style="btn-danger" id="alter_is_zero">
                                    <option>是</option>
                                    <option>否</option>
                                </select>
                            </div>
                        </div>

                        <div class="form-group has-success">
                            <label class="col-sm-2 control-label label-font" for="alter_user_select">管理员</label>
                            <div class="col-sm-10">
                                <select class="js-data-example-ajax form-control" id="alter_user_select" >
                                </select>
                            </div>
                        </div>

                        <div class="form-group has-success">
                            <label class="col-sm-2 control-label label-font" for="alter_address">仓库地址</label>
                            <div class="col-sm-10">
                                <textarea class="form-control" rows="4" id="alter_address"></textarea>
                            </div>
                        </div>

                        <div class="form-group has-success">
                            <label class="col-sm-2 control-label label-font" for="alter_use">仓库用途</label>
                            <div class="col-sm-10">
                                <textarea class="form-control" rows="4" id="alter_use"></textarea>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary" onclick="alter()">编辑
                    </button>
                </div>
            </div> <!--/.modal-content -->
        </div><!--/.modal-->
    </div>

<!-- JavaScript files-->
<!-- jquery.js -->
<script src="../../static/js/lib/jquery.min.js" th:src="@{/js/lib/jquery.min.js}"></script>
<!-- Bootstrap.js -->
<script src="../../static/js/lib/bootstrap3.4.1.min.js" th:src="@{/js/lib/bootstrap3.4.1.min.js}"></script>
<!-- Bootstrap-table.js -->
<script src="../../static/js/lib/bootstrap-table.min.js" th:src="@{/js/lib/bootstrap-table.min.js}"></script>
<script src="../../static/js/lib/bootstrap-table-zh-CN.min.js" th:src="@{/js/lib/bootstrap-table-zh-CN.min.js}"></script>
<!-- sweetalert2.js -->
<script src="../../static/js/lib/sweetalert.min.js" th:src="@{/js/lib/sweetalert.min.js}"></script>
<script src="../../static/js/lib/es6-promise.auto.min.js" th:src="@{/js/lib/es6-promise.auto.min.js}"></script>
<!-- jquery-ui.js -->
<script src="../../static/js/lib/jquery-ui.min.js" th:src="@{/js/lib/jquery-ui.min.js}"></script>
<!-- select2.js -->
<script src="../../static/js/lib/select2.min.js" th:src="@{/js/lib/select2.min.js}"></script>
<script src="../../static/js/lib/Select2-zh-CN.js" th:src="@{/js/lib/Select2-zh-CN.js}"></script>
<script src="../../static/js/common.js" th:src="@{/js/common.js}"></script>
<script src="../../static/js/dragModel.js" th:src="@{/js/dragModel.js}"></script>

<script>
    /** 初始化 */
    let $table = $('#table');
    let $userSelect = $("#user_select");
    let $alterUserSelect = $("#alter_user_select");

    // 服务器分页获取数据
    function getServerPaging(node, url, queryParams) {
        initTable(node, url, [10, 20, 30, 40, 50, 60, "All"], queryParams, [{
            checkbox: true
        }, {
            field: 'name',
            title: '仓库名称',
            width: '100px',
            formatter: stringFormatter
        }, {
            field: 'type',
            title: '仓库类型',
            width: '100px',
            formatter: stringFormatter
        }, {
            field: 'isZero',
            title: '是否零仓库',
            width: '100px',
            align: "center",
            formatter: stringFormatter
        }, {
            field: 'address',
            title: '仓库地址',
            width: '170px',
            formatter: stringFormatter
        }, {
            field: 'use',
            title: '仓库用途',
            width: '170px',
            formatter: stringFormatter
        }, {
            field: 'houseManager',
            title: '管理员',
            width: '90px',
            formatter: stringFormatter
        }, {
            field: 'createTime',
            title: '创建日期',
            width: '170px',
            formatter: dateFormatter
        }, {
            field: 'updateTime',
            title: '更新日期',
            width: '170px',
            formatter: dateFormatter
        }, {
            field: 'operation',
            title: '操作',
            width: '257px',
            formatter: operateFormatter // 自定义方法，添加操作按钮
        }]);
    }

    function operateFormatter(value, row, index) {  // 赋予的参数
        return [
            '<button class="btn btn-success view" data-toggle="modal" data-target="#myModal" onclick="view('+ index +');">查看</button>&nbsp;&nbsp;'+
            '<button class="btn btn-primary view" data-toggle="modal" data-target="#alterModal" onclick="initAlter('+ index +');">编辑</button>&nbsp;&nbsp;'+
            '<button class="btn btn-danger view"  onclick="deleteAll('+ index +');">删除</button>'
        ].join('');
    }

    dragTheModalDialog();

    changeToolTip();

    // 初始化新增下拉框
    initSelect($userSelect, "请输入管理员姓名", userFormatter, "/user/getUsers", 2000, params => {
        return { offset: params.page ? 10 * (params.page - 1) : 0, limit: 10, name: params.term };
    }, result => result.module.rows.map(item => {
        return { id: item.id, text: item.name, sex: item.sex};
    }), 10);

    // 初始化编辑下拉框
    initSelect($alterUserSelect, "请输入管理员姓名", userFormatter, "/user/getUsers", 2000, params => {
        return { offset: params.page ? 10 * (params.page - 1) : 0, limit: 10, name: params.term };
    }, result => result.module.rows.map(item => {
        return { id: item.id, text: item.name, sex: item.sex};
    }), 10);

    function userFormatter(result) {
        return '<span title="'+ ('1' === result.sex ? '男' : '女') + '">' + result.text +'</span>';
    }

    // 初始化数据信息
    function init() {
        getServerPaging($table, "/warehouse-management/getWarehouse", params => {
            return { limit: params.limit, offset: params.offset, sort: "create_time", order: "DESC" };
        });
    }
</script>

<script>
    /** 功能 */
    // 添加信息
    function addMsg() {
        if (0 !== $userSelect.select2('data').length) {
            let temp = {
                syUserId:  $userSelect.select2('data')[0].id,
                name: $("#txt_name").val(),
                type: $("#txt_type").val(),
                isZero: $("#txt_is_zero").val(),
                address: $("#txt_address").val(),
                use: $("#txt_use").val()
            };
            if ("" === temp.name || "--请输入--" === temp.type || "--请输入--" === temp.isZero) {
                swal("操作提示", "请输入必输入项", "info");
            } else {
                parameterPostRequest("/warehouse-management/insert", temp, () => {
                    $('#addModal').modal('hide');
                    swal("正确", "添加成功", "success");
                    init();
                });
            }
        } else {
            swal("操作提示", "请输入必输入项", "info");
        }
    }

    // 编辑信息
    // alter记录id
    let alterId;
    function initAlter(index) {
        let rows = $table.bootstrapTable('getData');
        $alterUserSelect.html('<option value="' + rows[index].syUserId + '">' + rows[index].houseManager + '</option>').trigger("change");
        $("#alter_name").val(rows[index].name).trigger("change");
        $("#alter_type").val(rows[index].type);
        $("#alter_is_zero").val(rows[index].isZero);
        $("#alter_address").val(rows[index].address);
        $("#alter_use").val(rows[index].use);
        alterId = rows[index].id;
    }
    function alter() {
        if (0 !== $alterUserSelect.select2('data').length) {
            let wareHouseDO = {
                id: alterId,
                name: $("#alter_name").val(),
                type: $("#alter_type").val(),
                isZero: $("#alter_is_zero").val(),
                address: $("#alter_address").val(),
                syUserId: $alterUserSelect.select2('data')[0].id,
                use: $("#alter_use").val()
            };
            if ("" === wareHouseDO.name || "" === wareHouseDO.type || "" === wareHouseDO.isZero) {
                swal("操作提示", "请输入必输入项", "info");
            } else {
                parameterPostRequest("/warehouse-management/updateWarehouse", wareHouseDO, () => {
                    $('#alterModal').modal('hide');
                    swal("正确", "编辑成功", "success");
                    init();
                });
            }
        } else {
            swal("操作提示", "请输入必输入项", "info");
        }
    }

    // 过滤
    function filter() {
        let data = {
            houseManager: $("#txt_search_house_manager").val(),
            name: $("#txt_search_name").val(),
            type: $("#txt_search_type").val(),
            isZero: $("#txt_search_is_zero").val()
        };

        if ("" === data["houseManager"] && "" === data["name"] && "--请选择--" === data["type"] && "--请选择--" === data["isZero"]) {
            swal("操作提示", "请填写过滤参数", "info");
            return;
        }
        getServerPaging($table, "/warehouse-management/getWarehouseByConditions", params => {
            let result = { limit: params.limit, offset: params.offset, sort: "create_time", order: "DESC", houseManager: data["houseManager"],
                name: data["name"], type: data["type"], isZero: data["isZero"]};

            removeEmptyField(result);
            if ("--请选择--" === result["type"]) {
                delete result["type"];
            }
            if ("--请选择--" === result["isZero"]) {
                delete result["isZero"];
            }

            return result;
        });
    }

    // 查看
    function view(index) {
        let rows = $table.bootstrapTable('getData');
        $("#td_name").text(rows[index].name);
        $("#td_type").text(rows[index].type);
        $("#td_is_zero").text(rows[index].isZero);
        $("#td_house_manager").text(rows[index].houseManager);
        $("#td_address").text(rows[index].address);
        $("#td_use").text(rows[index].use);
    }

    // 删除
    function deleteAll(index) {
        dialog("确定删除吗？", () => {
            let data = { "id": $table.bootstrapTable('getData')[index].id };
            parameterPostRequest("/warehouse-management/delete", data, () => {
                swal("正确", "删除成功", "success");
                init();
            });
        });
    }

</script>

</body>

</html>